package org.monster.context.core.communicator;

import org.monster.context.core.parameter.HttpResponse;

import javax.servlet.ServletOutputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.OutputStream;

/**
 * 应用服务者输入流
 *
 * @author Monster
 */
public class ApplicationServletOutputSteam extends ServletOutputStream {

    /**
     * 缓存输入流
     */
    private final BufferedOutputStream bos;
    /**
     * Http响应
     */
    private HttpResponse response;
    /**
     * 共计
     */
    private int count = -1;

    /**
     * 构造方法
     *
     * @param out 输出流
     */
    public ApplicationServletOutputSteam(OutputStream out) {
        bos = new BufferedOutputStream(out);

    }

    /**
     * 设置 响应
     *
     * @param response 响应
     */
    public void setResponse(HttpResponse response) {
        this.response = response;
    }

    /**
     * 写入
     *
     * @param b 整型
     * @throws IOException
     */
    @Override
    public void write(int b) throws IOException {
        bos.write(b);
        bos.flush();

    }

    /**
     * 打印
     *
     * @param s 字符串
     * @throws IOException 输入输出异常
     */
    @Override
    public void print(java.lang.String s) throws java.io.IOException {
        count++;
        if (count == 0) {
            response.setContentLength(s.length());
            response.sendHeaders();
        }
        super.print(s);
    }

    /**
     * 打印
     *
     * @param b 布尔
     * @throws IOException 输入输出异常
     */
    @Override
    public void print(boolean b) throws java.io.IOException {
        count++;
        if (count == 0) {
            response.setContentLength(1);
            response.sendHeaders();
        }
        super.print(b);

    }

    /**
     * 打印
     *
     * @param c 字符
     * @throws IOException 输入输出异常
     */
    @Override
    public void print(char c) throws java.io.IOException {
        count++;
        if (count == 0) {
            response.setContentLength(1);
            response.sendHeaders();
        }
        super.print(c);

    }

    /**
     * 打印
     *
     * @param i 整型
     * @throws IOException 输入输出异常
     */
    @Override
    public void print(int i) throws java.io.IOException {
        count++;
        if (count == 0) {
            response.setContentLength(1);
            response.sendHeaders();
        }
        super.print(i);

    }

    /**
     * 打印
     *
     * @param l 长整型
     * @throws IOException 输入输出异常
     */
    @Override
    public void print(long l) throws java.io.IOException {
        count++;
        if (count == 0) {
            response.setContentLength(1);
            response.sendHeaders();
        }
        super.print(l);
    }

    /**
     * 打印
     *
     * @param f 单精度浮点
     * @throws IOException 输入输出异常
     */
    @Override
    public void print(float f) throws java.io.IOException {
        count++;
        if (count == 0) {
            response.setContentLength(1);
            response.sendHeaders();
        }
        super.print(f);
    }

    /**
     * 打印
     *
     * @param d 双精度浮点
     * @throws IOException 输入输出异常
     */
    @Override
    public void print(double d) throws java.io.IOException {
        count++;
        if (count == 0) {
            response.setContentLength(1);
            response.sendHeaders();
        }
        super.print(d);
    }

    /**
     * 打印
     *
     * @throws IOException 输入输出异常
     */
    @Override
    public void println() throws java.io.IOException {
        count++;
        if (count == 0) {
            response.setContentLength(0);
            response.sendHeaders();
        }
        super.println();
    }

    /**
     * 打印
     *
     * @param s 字符串
     * @throws IOException 输入输出异常
     */
    @Override
    public void println(java.lang.String s) throws java.io.IOException {
        count++;
        if (count == 0) {
            response.setContentLength(s.length());
            response.sendHeaders();
        }
        super.println(s);
    }

    /**
     * 打印
     *
     * @param b 布尔
     * @throws IOException 输入输出异常
     */
    @Override
    public void println(boolean b) throws java.io.IOException {
        count++;
        if (count == 0) {
            response.setContentLength(1);
            response.sendHeaders();
        }
        super.println(b);
    }

    /**
     * 打印
     *
     * @param c 字符串
     * @throws IOException 输入输出异常
     */
    @Override
    public void println(char c) throws java.io.IOException {

        count++;
        if (count == 0) {
            response.setContentLength(1);
            response.sendHeaders();
        }
        super.println(c);
    }

    /**
     * @param i 整型
     * @throws IOException 输入输出异常
     */
    @Override
    public void println(int i) throws java.io.IOException {
        count++;
        if (count == 0) {
            response.setContentLength(1);
            response.sendHeaders();
        }
        super.println(i);
    }

    /**
     * 打印
     *
     * @param l 长整型
     * @throws IOException 输入输出异常
     */
    @Override
    public void println(long l) throws java.io.IOException {
        count++;
        if (count == 0) {
            response.setContentLength(1);
            response.sendHeaders();
        }
        super.println(l);
    }

    /**
     * 打印
     *
     * @param f 单精度浮点
     * @throws IOException 输入输出异常
     */
    @Override
    public void println(float f) throws java.io.IOException {
        count++;
        if (count == 0) {
            response.setContentLength(1);
            response.sendHeaders();
        }
        super.println(f);
    }

    /**
     * 打印
     *
     * @param d 双精度浮点
     * @throws IOException 输入输出异常
     */
    @Override
    public void println(double d) throws java.io.IOException {
        count++;
        if (count == 0) {
            response.setContentLength(1);
            response.sendHeaders();
        }
        super.println(d);
    }

}
